package com.visionblue.business.ship.page.collection.panel.flow

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import com.visionblue.business.ship.R
import com.visionblue.business.ship.util.ext.setVisible
import kotlinx.android.synthetic.main.view_flow.view.*

/**
 * @author: xlh
 * @date: 2020-07-14 3:29 PM
 * @desc:
 */
class FlowViewLayout(context: Context, attrs: AttributeSet? = null) :
    ConstraintLayout(context, attrs) {

    private var currState = IDLE
    var onSwitchClickListener: OnSwitchClickListener? = null

    init {
        LayoutInflater.from(context).inflate(R.layout.view_flow, this)
        showIdle()
        setOnClickListener {
            if (onSwitchClickListener?.onSwitchClickResult(currState) == false) {
                when (currState) {
                    IDLE -> {
//                        showWorking()
                        onSwitchClickListener?.onStart()
                    }
                    WORKING -> {
                        showIdle()
                        onSwitchClickListener?.onStop()
                    }
                    DISABLE -> {

                    }
                }
            }
        }
    }

    fun setTypeText(typeText: CharSequence) {
        tvTypeText.text = typeText
    }

    fun setState(state: Int) {
        when (state) {
            WORKING -> showWorking()
            IDLE -> showIdle()
            DISABLE -> showDisable()
        }
    }

    fun getCurrState() = currState

    fun showWorking(volume: CharSequence? = null, speed: CharSequence? = null) {
        if (currState != WORKING) {
            currState = WORKING
            ivSwitch.setImageResource(R.drawable.ic_flow_stop)
            tvStartDesc.setVisible(false)
//            tvVolume.setVisible(true)
//            tvVolumeDesc.setVisible(true)
            tvStopDesc.setVisible(true)
            tvVolumeSpeed.setVisible(true)
            setBackgroundResource(R.drawable.bg_rectround__orangeff_8dp)
            tvStartDesc.setTextColor(ContextCompat.getColor(context, R.color.black3))
            tvTypeText.setTextColor(ContextCompat.getColor(context, R.color.black3))
        }
//        tvVolume.text = volume
        tvVolumeSpeed.text = speed
//        tvVolume.setVisible(false)
    }

    fun showIdle() {
        if (currState != IDLE) {
            currState = IDLE
            ivSwitch.setImageResource(R.drawable.ic_flow_start)
            tvStartDesc.setVisible(true)
            tvVolumeSpeed.setVisible(false)
//            tvVolumeDesc.setVisible(false)
//            tvVolume.setVisible(false)
            tvStopDesc.setVisible(false)
            tvStartDesc.setTextColor(ContextCompat.getColor(context, R.color.black3))
            tvTypeText.setTextColor(ContextCompat.getColor(context, R.color.black3))
            setBackgroundResource(R.drawable.bg_rectround_white_8dp)
        }
    }

    fun showDisable() {
        if (currState != DISABLE) {
            currState = DISABLE
            tvStartDesc.setVisible(true)
            tvVolumeSpeed.setVisible(false)
//            tvVolumeDesc.setVisible(false)
//            tvVolume.setVisible(false)
            tvStopDesc.setVisible(false)
            setBackgroundResource(R.drawable.bg_rectround_greye6_8dp)
            tvStartDesc.setTextColor(ContextCompat.getColor(context, R.color.greycc))
            tvTypeText.setTextColor(ContextCompat.getColor(context, R.color.greycc))
            ivSwitch.setImageResource(R.drawable.ic_start_disable)
        }
    }

    abstract class OnSwitchClickListener {
        open fun onSwitchClickResult(currState: Int): Boolean = false
        abstract fun onStart()
        abstract fun onStop()
    }

    companion object {
        const val IDLE = 1
        const val WORKING = 2
        const val DISABLE = 3
    }
}